package com.xyz.apis.modules.api.service.impl;


import com.xyz.admin.common.entity.EvUserEntity;
import com.xyz.apis.common.utils.CommonUtil;
import com.xyz.apis.common.utils.SnowflakeIdWorker;
import com.xyz.apis.modules.api.dto.UserDTO;
import com.xyz.apis.modules.api.mapper.EvUserMapper;
import com.xyz.apis.modules.api.service.EvUserService;
import com.xyz.apis.repository.RedisRepository;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class EvUserServiceImpl implements EvUserService {

    protected Logger logger = LoggerFactory.getLogger(EvUserServiceImpl.class);

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    private EvUserMapper evUserMapper;



    @Autowired
    private RedisRepository redisRepository;


    @Override
    public EvUserEntity queryByPhone(String phone, Long userid) {
        return evUserMapper.queryByPhone(phone,null,userid);
    }

    @Override
    public EvUserEntity queryByPhoneAccount(String phone, String account, Long userid) {

        return evUserMapper.queryByPhone(phone,account,userid);
    }

    @Override
    public String changePassword(Long userid, String phone, String password) {
        EvUserEntity userEntity = new EvUserEntity();
        if (CommonUtil.moreThanZero(userid)) {
            userEntity.setUserid(userid);
        }
        if (!CommonUtil.isBlank(phone) && 11 == phone.length()) {
            userEntity.setPhone(phone);
        }
        String salt = RandomStringUtils.randomAlphanumeric(20);
        userEntity.setPassword(new Sha256Hash(password, salt).toHex());
        userEntity.setSalt(salt);
        userEntity.setFirst(1);
        int count = evUserMapper.changePassword(userEntity);
        if (0 < count) {
            return "";
        } else {
            return "修改失败";
        }
    }

    @Override
    public UserDTO queryUserById(Long userid) {
        EvUserEntity evUserEntity = evUserMapper.queryObjectById(userid);
        UserDTO dto = new UserDTO();
        if(null!=evUserEntity){
            dto.setAvatar(evUserEntity.getAvatar());
            dto.setNickName(evUserEntity.getNickname());
            dto.setJifen(BigDecimal.ZERO);
            if(evUserEntity.getJifen().compareTo(BigDecimal.ZERO)==1) {
                dto.setJifen(evUserEntity.getJifen());
            }
            dto.setConsumeJifen(BigDecimal.ZERO);
            if(evUserEntity.getConsumeJifen().compareTo(BigDecimal.ZERO)==1) {
                dto.setConsumeJifen(evUserEntity.getConsumeJifen());
            }
            dto.setReserved5(evUserEntity.getReserved5());
        }
        return dto;
    }

    @Override
    public int updateUserinfo(Long userid,String nickname, String avatar) {
        EvUserEntity evUserEntity = new EvUserEntity();
        evUserEntity.setUserid(userid);
        evUserEntity.setNickname(nickname);
        if(null!=avatar){
            evUserEntity.setAvatar(avatar);
        }

        return evUserMapper.changePassword(evUserEntity);
    }

    @Override
    public String updatepassword(Long userid, String password, String newpassword) {
        EvUserEntity entity = evUserMapper.querypassword(userid);
        String oldPasswordHex = new Sha256Hash(password, entity.getSalt()).toHex();
        if(!(oldPasswordHex).equals(entity.getPassword())){
            return "修改密码失败，原密码错误";
        }
        EvUserEntity userEntity = new EvUserEntity();
        userEntity.setUserid(userid);
        userEntity.setPassword(new Sha256Hash(newpassword, entity.getSalt()).toHex());
        int count=evUserMapper.updateByPrimaryKeySelective(userEntity);
        if(1>count){
            return "修改失败";
        }
        return "";
    }


    @Override
    @Transactional
    public int insertData(EvUserEntity entity, EvUserEntity pidEntity) {
        entity.setFirst(0);//设定为从未登陆
        int count = evUserMapper.insertSelective(entity);
        return 1;
    }

    @Override
    public EvUserEntity insertUserData(EvUserEntity entity, EvUserEntity pidEntity) {
        return null;
    }

    @Override
    public int updateSynchronUserId(Long userid, Long zUserid) {
        // 更新数据
        EvUserEntity userEntity = new EvUserEntity();
        userEntity.setReserved3(zUserid.toString());
        userEntity.setUserid(userid);
        return evUserMapper.updateByPrimaryKeySelective(userEntity);
    }

    @Override
    @Transactional
    public int updateSynchronInvitation(Long userid, Long jybUserid) {
        return 1;
    }




}
